<template>
  <div id="wishUploadPage">
    <el-form :model="wishData" :rules="formRules" ref="form">
      <el-form-item prop="name">
        <div>
          愿望名称：
          <el-input v-model="wishData.name" type="input" placeholder="请输入愿望名称" clearable></el-input>
        </div>
      </el-form-item>
      <el-form-item prop="type">
        <div>
          愿望类型：
          <div class="typeBar">
            <p>1.选择已有的愿望类型</p>
            <el-select v-model="wishData.type">
              <el-option v-for="(item, index) in typeData" :key="index" :value="item" :label="item"></el-option>
            </el-select>
            <p>2.创建新的愿望类型</p>
            <el-input v-model="wishData.type" type="input" placeholder="请输入愿望类型" clearable></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <div>
          描述 (选填)：
          <el-input type="textarea" v-model="wishData.description" placeholder="请输入描述内容" autosize></el-input>
        </div>
      </el-form-item>
      <el-form-item prop="time">
        <div>
          时间：
          <el-date-picker id="el_input" type="date" placeholder="请选择许愿日期" v-model="wishData.time"></el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <el-button @click="submit" type="success" style="z-index: 20;">提交</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      wishData: {
        name: null,
        time: new Date(),
        type: null,
        completed: false,
        description: null,
      }, // 许愿数据
      typeData: [], // 愿望类型数据
      formRules: {
        name: [
          { required: true, message: '请输入愿望名称', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入愿望类型', trigger: 'blur' },
          { required: true, message: '请输入愿望类型', trigger: 'change' },
        ],
        time: [
          { type: 'date', required: true, message: '请输入许愿时间', trigger: 'change' },
        ]
      }, // 表单规则数据
    }
  },
  methods: {
    // 获取愿望类型
    getAllWishType() {
      this.$axios({
        method: 'post',
        url: '/wish/login/getAllType'
      }).then(res => {
        this.typeData = res.data
      })
    },
    // 许愿事件
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 发送请求
          let data = new FormData()
          data.append("name", this.wishData.name)
          data.append("time", this.wishData.time)
          data.append("type", this.wishData.type)
          if (this.wishData.description != null && this.wishData.description != '') data.append("description", this.wishData.description)
          this.$axios({
            method: 'post',
            url: 'wish/login/uploadWish',
            data: data,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            if (res.data) {
              this.$message({
                type: 'success',
                message: '提交成功',
                showClose: true,
              })
            } else {
              this.$message({
                type: 'error',
                message: '提交失败！出现错误了X.X',
                showClose: true,
              })
            }
          })
        } else {
          this.$message({
            message: '请完善愿望数据再提交',
            type: 'error',
            showClose: true,
          })
          return false
        }
      })

    }
  },
  mounted() {
    this.getAllWishType()
  }
}
</script>

<style lang="scss" scoped>
#wishUploadPage {
  display: flex;
  flex-direction: column;
  padding: 20px;
}
</style>
<style scoped="scoped" scope>
/deep/.el-textarea__inner {
  min-height: 60px !important;
}

/deep/.el-input__inner {
  width: 300px;
}

/deep/.el-textarea__inner,
/deep/.el-input__inner {
  background-color: #202020;
  text-align: center;
  color: #fff;

}
</style>